package com.adv.ems.controller.admin;

import com.adv.ems.constants.SystemConst;
import com.adv.ems.model.dto.APIRequest;
import com.adv.ems.model.dto.APIResponse;
import com.adv.ems.model.param.BaseQueryParam;
import com.adv.ems.model.vo.ReportTemplateVO;
import com.adv.ems.service.ReportTemplateService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/ems/client/base/report/template")
public class ReportTemplateController {
    @Resource
    ReportTemplateService reportTemplateService;

    @PostMapping("/list")
    public APIResponse<ReportTemplateVO> list(@RequestHeader(name = SystemConst.Header.ACCEPT_LANGUAGE, defaultValue = SystemConst.Header.ZH_CN) String acceptLanguage,
                                              @RequestBody APIRequest<BaseQueryParam> request) {
        return APIResponse.<ReportTemplateVO>build().content(reportTemplateService.list(request.getContent(), acceptLanguage));
    }

    @PostMapping("/update")
    public APIResponse<Boolean> update(@RequestHeader(name = SystemConst.Header.ACCEPT_LANGUAGE, defaultValue = SystemConst.Header.ZH_CN) String acceptLanguage,
                                       @RequestParam(name = "file") MultipartFile file,
                                       @RequestParam(name = "name", required = false) String name) {
        return APIResponse.<Boolean>build().content(reportTemplateService.update(file, name, acceptLanguage));
    }

    @RequestMapping("/download/file")
    public void update(@RequestParam(name = "id") Long id, HttpServletResponse response) {
        reportTemplateService.downloadFile(id, response);
    }

    @PostMapping("/delete")
    public APIResponse<Boolean> delete(@RequestParam(name = "id") Long id) {
        return APIResponse.<Boolean>build().content(reportTemplateService.delete(id));
    }
}
